package edu.sctu.yueding.activitys;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import edu.sctu.yueding.R;
import edu.sctu.yueding.adapter.ScanResultGetTasksAdapter;
import edu.sctu.yueding.bean.JsonBaseBeen;
import edu.sctu.yueding.bean.JsonBaseListBean;
import edu.sctu.yueding.bean.PointBean;
import edu.sctu.yueding.bean.TaskBean;
import edu.sctu.yueding.bean.TaskListBean;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.ProgressUtil;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;
import edu.sctu.yueding.utils.httputils.SharePreference;

/**
 * Created by zcy on 2016/8/21.
 */
public class ScanResultGetTasksActivity extends BaseActivity {

    private ListView scanResultListView;
    private TextView mBack;
    private String scanResult;
    private Activity activity = this;

    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_scan_result_get_tasks);
        initSystemBar(this, 0);

        initView();

    }

    private void initView() {

        mBack = (TextView) findViewById(R.id.id_scan_result_get_tasks_back_txt);
        scanResultListView = (ListView) findViewById(R.id.id_scan_result_list_view);
        progressUtil = ProgressUtil.getInstance(activity, null);

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("flag", 3);
                JumpUtils.activityToFragmentJump(activity, bundle);
            }
        });
        scanResult = getIntent().getStringExtra("result");

        if (scanResult != null && !scanResult.equals("")) {

            progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_SPINNER, true);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    srartInternet(scanResult);
                }
            }, 1000);
        }

    }

    private void srartInternet(String result) {

        GetVolleyHttp.getInstance(activity).getStringGETJsonData(result, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Log.e("rrrr", response);

                JsonBaseBeen<TaskListBean<TaskBean>> markBeanJsonBaseBeen = JSON.parseObject(response, new TypeReference<JsonBaseBeen<TaskListBean<TaskBean>>>() {
                }.getType());

                if (markBeanJsonBaseBeen.getMeta().code == 200){

                    ScanResultGetTasksAdapter adapter = new ScanResultGetTasksAdapter(activity, markBeanJsonBaseBeen.getData().getContent());
                    scanResultListView.setAdapter(adapter);
                }else {

                    Bundle bundle = new Bundle();
                    bundle.putString("errorMsg","请认真核对您需要扫描的二维码！");
                    JumpUtils.jumpActivity(activity,ScanErrorActivity.class,bundle);
                    activity.finish();

                }
                progressUtil.dismiss(progressDialog);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.e("rrrr", "yyyyy");
                progressUtil.dismiss(progressDialog);
                Toast.makeText(activity, "网络出了点问题！", Toast.LENGTH_SHORT).show();
            }
        }, "Bearer " + SharePreference.getInstance(activity).getString(CacheConfig.CACHE_TOKEN));

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {

            Bundle bundle = new Bundle();
            bundle.putInt("flag",3);
            JumpUtils.activityToFragmentJump(activity, bundle);

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



}
